//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.bean.RoleBean;
import net.mingsoft.basic.biz.IModelBiz;
import net.mingsoft.basic.biz.IRoleBiz;
import net.mingsoft.basic.biz.IRoleModelBiz;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.entity.ManagerEntity;
import net.mingsoft.basic.entity.RoleEntity;
import net.mingsoft.basic.entity.RoleModelEntity;
import net.mingsoft.basic.util.BasicUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-基础接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/basic/role"})
public class RoleAction extends BaseAction {
    @Autowired
    private IRoleBiz roleBiz;
    @Autowired
    private IModelBiz modelBiz;
    @Autowired
    private IRoleModelBiz roleModelBiz;

    public RoleAction() {
    }

    @ApiIgnore
    @GetMapping({"/index"})
    @RequiresPermissions({"basic:role:view"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/WEB-INF/manager/basic/role/index";
    }

    @ApiOperation("返回编辑界面role_form")
    @ApiImplicitParam(
            name = "id",
            value = "角色ID",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/form"})
    @RequiresPermissions({"basic:role:view"})
    public String form(@ModelAttribute @ApiIgnore RoleEntity role, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isNotEmpty(role.getId())) {
            RoleEntity roleEntity = (RoleEntity)this.roleBiz.getById(role.getId());
            model.addAttribute("roleEntity", roleEntity);
        }

        return "/WEB-INF/manager/basic/role/form";
    }

    @ApiOperation("查询角色列表")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "roleName",
            value = "角色名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerId",
            value = "该角色的创建者ID",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/list"})
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore RoleEntity role, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        List roleList = this.roleBiz.query(role);
        return ResultData.build().success(new EUListBean(roleList, (int)BasicUtil.endPage(roleList).getTotal()));
    }

    @ApiOperation("根据角色ID查询模块集合")
    @ApiImplicitParam(
            name = "roleId",
            value = "角色ID",
            required = true,
            paramType = "path"
    )
    @GetMapping({"/{roleId}/queryByRole"})
    @ResponseBody
    public ResultData queryByRole(@PathVariable @ApiIgnore int roleId, HttpServletResponse response) {
        List models = this.modelBiz.queryModelByRoleId(roleId);
        return ResultData.build().success(models);
    }

    @ApiOperation("查询所有角色列表")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "roleName",
            value = "角色名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerId",
            value = "该角色的创建者ID",
            required = false,
            paramType = "query"
    )})
    @GetMapping({"/all"})
    @ResponseBody
    public ResultData all(@ModelAttribute @ApiIgnore RoleEntity role, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        List roleList = this.roleBiz.query(role);
        return ResultData.build().success(new EUListBean(roleList, (int)BasicUtil.endPage(roleList).getTotal()));
    }

    @ApiOperation("获取角色")
    @ApiImplicitParam(
            name = "id",
            value = "角色ID",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/get"})
    @RequiresPermissions({"basic:role:view"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore RoleEntity role, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isEmpty(role.getId())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("role.id")}));
        } else {
            RoleEntity _role = (RoleEntity)this.roleBiz.getEntity(Integer.parseInt(role.getId()));
            return ResultData.build().success(_role);
        }
    }

    @ApiOperation("保存角色实体")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "roleName",
            value = "角色名称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "managerId",
            value = "该角色的创建者ID",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "ids",
            value = "功能权限id",
            required = true,
            paramType = "query"
    )})
    @LogAnn(
            title = "保存角色实体",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/saveOrUpdateRole"})
    @ResponseBody
    @RequiresPermissions(
            value = {"basic:role:save", "basic:role:update"},
            logical = Logical.OR
    )
    public ResultData saveOrUpdateRole(@ModelAttribute @ApiIgnore RoleBean role, HttpServletResponse response, HttpServletRequest request) {
        RoleBean _role = new RoleBean();
        _role.setRoleName(role.getRoleName());
        ManagerEntity managerSession = BasicUtil.getManager();
        if (StringUtils.isEmpty(role.getRoleName())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("roleName")}));
        } else {
            RoleBean roleBean = (RoleBean)this.roleBiz.getEntity(_role);
            if (StringUtils.isNotEmpty(role.getId())) {
                if (roleBean != null && !roleBean.getId().equals(role.getId())) {
                    return ResultData.build().error(this.getResString("roleName.exist"));
                }

                role.setUpdateBy(BasicUtil.getManager().getId());
                role.setUpdateDate(new Date());
                role.setCreateBy((String)null);
                this.roleBiz.updateById(role);
            } else {
                if (roleBean != null) {
                    return ResultData.build().error(this.getResString("roleName.exist"));
                }

                role.setCreateBy(BasicUtil.getManager().getId());
                role.setCreateDate(new Date());
                this.roleBiz.save(role);
                this.roleBiz.updateCache();
            }

            List<RoleModelEntity> roleModelList = new ArrayList();
            if (!StringUtils.isEmpty(role.getIds())) {
                String[] var8 = role.getIds().split(",");
                int var9 = var8.length;

                for(int var10 = 0; var10 < var9; ++var10) {
                    String id = var8[var10];
                    RoleModelEntity roleModel = new RoleModelEntity();
                    roleModel.setRoleId(Integer.parseInt(role.getId()));
                    roleModel.setModelId(Integer.parseInt(id));
                    roleModelList.add(roleModel);
                }

                this.roleModelBiz.deleteByRoleId(Integer.parseInt(role.getId()));
                this.modelBiz.updateCache();
                this.roleModelBiz.saveBatch(roleModelList, roleModelList.size());
            } else {
                this.roleModelBiz.deleteByRoleId(Integer.parseInt(role.getId()));
            }

            this.roleBiz.updateCache();
            return ResultData.build().success(role);
        }
    }

    @ApiOperation("批量删除角色")
    @PostMapping({"/delete"})
    @ResponseBody
    @RequiresPermissions({"basic:role:del"})
    @LogAnn(
            title = "删除角色",
            businessType = BusinessTypeEnum.DELETE
    )
    public ResultData delete(@RequestBody List<RoleEntity> roles, HttpServletResponse response, HttpServletRequest request) {
        ManagerEntity managerSession = BasicUtil.getManager();
        return this.roleBiz.deleteRoleByRoles(roles, managerSession) ? ResultData.build().success("删除成功", (Object)null) : ResultData.build().success("删除成功，已过滤当前不可删除角色", (Object)null);
    }
}
